package b1.controls.basic
{
	import b1.conifg.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;

	public class MessageBoxWithIcon extends Sprite
	{
		public var buttonOK:BasicButton;
		public var buttonCacel:BasicButton;

		public function MessageBoxWithIcon(message:String, iconImg:String, iconName:String = null, alpha:Number = 1, hasCacelButton:Boolean = false)
		{
			super();
			var containerSprite:Sprite = new Sprite();
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("MessageBox1");
			var mainSprite:Sprite = Sprite(new MessageBoxGgr());
			mainSprite.alpha = alpha;
			containerSprite.addChild(mainSprite);
			
			//头像
			var Icon:Class;
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
			iconName = iconName.replace(/self/, me.name);
			message = message.replace(/self/, me.name);
			var icon:Sprite;
			//头像遮罩
			var IconMask:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var iconMask:Sprite = new IconMask();
			iconMask.width = 100;
			iconMask.height = 100;
			iconMask.x = 40;
			iconMask.y = 60;
			if (iconImg == "self" || iconImg.substr(0, 3) == "id:")
			{
				var animalInfo:AnimalInfo;
				if (iconImg == "self")
				{
					animalInfo = myAnimal;
				}
				else
				{
					var animalId:int = parseInt(iconImg.substr(3));
					animalInfo = PlayerInfoHelper.getInstance().animals[animalId];
				}
				Icon = ResourceHelper.getInstance().getClassByName("Idle" + animalInfo.animalIndex);
				icon = new BasicMc(Icon, 0);
//				var iconScale:Number = 8000 / (icon.width * icon.width + icon.height * icon.height);
				icon.height = 100;
				icon.scaleX = icon.scaleY;
				icon.x = iconMask.x + iconMask.width / 2;
				icon.y = 70 + icon.height;
			}
			else
			{
				Icon = ResourceHelper.getInstance().getClassByName(iconImg);
				icon = new Icon();
				icon.width = 100;
				icon.scaleY = icon.scaleX;
				icon.x = 40;
				icon.y = 60; 
			}
			containerSprite.addChild(icon);
			icon.mask = iconMask;
			containerSprite.addChild(iconMask);
			if (null != iconName)
			{
				var nameText:TextField = new TextField();
				nameText.selectable = false;
	        	nameText.autoSize = TextFieldAutoSize.CENTER;
				nameText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
				nameText.text = iconName;
				nameText.x = iconMask.x + (iconMask.width - nameText.width) / 2;
				nameText.y = iconMask.y + iconMask.width + 5;
				containerSprite.addChild(nameText);
			}
			//文字
			var messageText:TextField = new TextField();
			messageText.selectable = false;
        	messageText.autoSize = TextFieldAutoSize.LEFT;
			messageText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			messageText.width = 200;
			messageText.wordWrap = true;
			messageText.text = message;
			messageText.x = iconMask.x + iconMask.width + 15;
			messageText.y = 85;
			containerSprite.addChild(messageText);
			
			//确定按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			buttonOK = new BasicButton(ButtonBgr, "确定");
			if (!hasCacelButton)
			{
				buttonOK.x = (mainSprite.width  - buttonOK.width) / 2;
			}
			else
			{
				buttonOK.x = mainSprite.width / 2 - buttonOK.width + 30;
			}
			buttonOK.y = mainSprite.height - buttonOK.height - 60;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			containerSprite.addChild(buttonOK);
			if (hasCacelButton)
			{
				buttonCacel = new BasicButton(ButtonBgr, "取消");
				buttonCacel.x = mainSprite.width / 2 + 50;
				buttonCacel.y = buttonOK.y;
				containerSprite.addChild(buttonCacel);
				buttonCacel.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			}
			containerSprite.x = -containerSprite.width / 2;
			containerSprite.y = -containerSprite.height / 2;
			addChild(containerSprite);
		}
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			close();
			StoryHelper.getInstance().nextStep();
			//添加新手导引
			if (SceneContainer.getInstance().step == 2 )
			{
				SceneContainer.getInstance().addStep();
			}
			else if (SceneContainer.getInstance().step == 17)
			{
				SceneContainer.getInstance().addStep();
			}
			else if (SceneContainer.getInstance().step == 12 || SceneContainer.getInstance().step == 13
				|| SceneContainer.getInstance().step == 14 || SceneContainer.getInstance().step == 15
				|| SceneContainer.getInstance().step == 16 || SceneContainer.getInstance().step == 28
				|| SceneContainer.getInstance().step == 29 || SceneContainer.getInstance().step == 30
				|| SceneContainer.getInstance().step == 31)
			{
				SceneContainer.getInstance().clearLastStep();
			}
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
		
	}
}